package com.zghhost.codedemo.zp;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author guohua.zhang@zhaopin.com.cn
 * @Date 2019/3/4 10:38
 */
public class ExportZpAddBook {
    public static void main(String[] args)throws  Exception{
        File file = new File("/Users/wow/jianguo_cloud/zhaipin_work/智联招聘通讯录.csv");
        if(file.exists()){
            file.delete();
        }
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
        bw.write("姓名,性别,部门,职位,上级,邮箱,电话\n");

        int i=1;
        while(true){
            String url = "http://zzbs.zhaopin.com/servlet/findContactList?action=findContactList&deptno=01&totalRows=966&cpage="+ i++;
            URL u = new URL(url);
            HttpURLConnection uc = (HttpURLConnection)u.openConnection();
            uc.setRequestProperty("Cookie","BIGipServernginx_pools=620865728.20480.0000; cod=; ZPSSO_USER_EMPID=13075301; ZPSSO_USER_NAME=guohua.zhang; ZPSSO_USER_INFO=%7B%22deptName%22%3A%22ZhaoPin+Ltd.%22%2C%22empRole%22%3A%220%22%2C%22isLeader%22%3Afalse%2C%22rdRole%22%3A%220%22%2C%22mngDeptId%22%3A%221%22%2C%22empSpirit%22%3A%220%22%2C%22extension%22%3A%22%22%2C%22ccName%22%3A%22default%22%2C%22deptId%22%3A%221%22%2C%22branchCompanyMngDeptId%22%3A%220%22%2C%22branchCompanyMngDeptName%22%3A%22%E9%9B%86%E5%9B%A2%E6%80%BB%E9%83%A8%22%2C%22salesLevel%22%3A%220%22%2C%22inCallinList%22%3Afalse%7D; ZPSSO_USER_PWD=duLfGp%2BpPulwPI9hLCn0pg%3D%3D; sts_deviceid=1693e10ff60651-0949d0a21e85ba-36657105-1764000-1693e10ff6163f; sts_sg=1; sts_chnlsid=Unknown; dywea=95841923.1366102094038516500.1551524496.1551524496.1551524496.1; dywec=95841923; dywez=95841923.1551524496.1.1.dywecsr=(direct)|dyweccn=(direct)|dywecmd=(none)|dywectr=undefined; __utma=269921210.741964027.1551524496.1551524496.1551524496.1; __utmc=269921210; __utmz=269921210.1551524496.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); index=2; at=ee01948fef9b43258a74c9249b4f3792; Token=ee01948fef9b43258a74c9249b4f3792; rt=a498284d99c7423fb108c621c68bf364; RDsUserInfo=\"\"; uiioit=\"\"; puid=1023112333; login_point=101265547; NTKF_T2D_CLIENTID=guest122BA771-831B-EBC7-D18E-3E11762D42D7; nTalk_CACHE_DATA={uid:kf_9051_ISME9754_12007036,tid:1551524525613228}; zp_src_url=https%3A%2F%2Fihr.zhaopin.com%2FloginPoint%2Fchoose.do%3Fbkurl%3Dhttps%253A%252F%252Frd5.zhaopin.com%252F; diagnosis=0; INNER_AUTHENTICATION=QmVhcmVyIGV5SmhiR2NpT2lKSVV6VXhNaUo5LmV5SnpkV0lpT2lJeE16QTNOVE13TVNJc0ltVjRjQ0k2TVRVMU1qTTRPVEU0T1gwLi0tUFV6QTZXNTJzeGR6SmVBeVJ0Z3ZxRTBIdUJpZmIyenRqQTZQS09jY2hmeGRMd3FNeXA5T1VPUGhjeDVtSFhNT094UEZlY2xEODdkMkxFaWlHWkVn; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%22665572533%22%2C%22%24device_id%22%3A%221693e10ff4d525-02fb6bd8e4bee9-36657105-1764000-1693e10ff4ed32%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24latest_referrer_host%22%3A%22%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%7D%2C%22first_id%22%3A%221693e10ff4d525-02fb6bd8e4bee9-36657105-1764000-1693e10ff4ed32%22%7D; JSESSIONID=165EE1C09795B28D8345076740FF15F4; csd=01");
            uc.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream(),"GBK"));

            StringBuffer sb = new StringBuffer();
            String line = null;
            while((line = br.readLine()) != null){
                sb.append(line);
            }

            br.close();
            uc.disconnect();

            Document doc = Jsoup.parse(sb.toString());

            Elements emts = doc.select("tr[bgcolor=#FFFFFF]");

            if(emts.size() == 0){
                break;
            }

            Pattern phonep = Pattern.compile("<!--[^>]*>(.*)<[^>]*>");
            for(Element emt : emts){
                String name = emt.select("td").eq(1).html();//姓名
                String sex = emt.select("td").eq(5).html();//性别
                String dep = emt.select("td").eq(8).html();//部门
                String job = emt.select("td").eq(3).html();//职位
                String up = emt.select("td").eq(7).html();//上级
                String email = emt.select("td").eq(6).html();//邮箱
                String phone = null;

                Matcher matcher = phonep.matcher(emt.toString());
                if(matcher.find()){
                    phone = matcher.group(1);
                }

                bw.write(String.format("%s,%s,%s,%s,%s,%s,%s\n",name,sex,dep,job,up,email,phone));
            }

        }

        bw.flush();
        bw.close();
    }
}
